package amadeus

import (
	"github.com/goccy/go-json"
	"lib/analysis-pnr/travelskypnr"
	"lib/log"
	omodel "ordercenter/model"
	"shopping/model"
	"strings"
)

type PnrItemObj interface {
	IsMatch(line string) bool
	Add(line string)
	Append(line string)
	Data() interface{}
	Name() string
}

type PNR struct {
	Text   string
	Code   string
	ObjAry []PnrItemObj
}

func NewPNR(code string) *PNR {

	ret := &PNR{
		Text: code,
	}
	ret.ObjAry = append(ret.ObjAry, NewPassengerItem(), NewFltSegmentItem(), NewOtherItem())
	return ret
}

func (p *PNR) Analysis() {
	p.Text = strings.TrimSpace(p.Text)
	if p.Text == "" {
		return
	}
	lines := strings.Split(p.Text, "\n")
	//第一行必须是--- TST开头
	if strings.HasPrefix(lines[0], "---") == false {
		return
	}
	//第二、三行为基本信息，从第二行取PNR编码
	l := strings.TrimSpace(lines[1])
	p.Code = l[len(l)-6:]

	eachItem(lines[3:], p.ObjAry)
}

func (p *PNR) Ouput() (result GetAmadeusInfo) {
	rev := make(map[string]interface{})
	rev["code"] = p.Code
	for _, o := range p.ObjAry {
		n := o.Name()
		if n != "" {
			rev[n] = o.Data()
		}
	}
	revJson, err := json.Marshal(rev)
	if err != nil {
		log.Errorf("err: %v", err)
	}
	if errs := json.Unmarshal(revJson, &result); errs != nil {
		log.Errorf("err: %v", errs)
	}
	return
}

func eachItem(strAry []string, objs []PnrItemObj) {

	var current PnrItemObj
	for _, row := range strAry {
		row = strings.TrimSpace(row)
		for _, o := range objs {
			if o.IsMatch(row) {
				current = o
				o.Add(row)
				row = ""
				break
			}
		}
		if row != "" {
			current.Append(row)
		}
	}
}

func HandleFlightDetail(data GetAmadeusInfo) (flightDetail *model.PnrDetail) {
	airline := ""
	journeys := make([]model.FlightListForPnr, 0)
	for _, item := range data.FlightSection {
		if airline == "" {
			airline = item.MarketingAirline
		}
		journeys = append(journeys, model.FlightListForPnr{
			Airline:  item.MarketingAirline,
			FlightNo: item.FlightNumber,
			DepCode:  item.DepartureCityCode,
			ArrCode:  item.ArrivalCityCode,
			DepDate:  item.DepartureDate + " " + item.DepartureTime,
			ArrDate:  item.ArrivalDate + " " + item.ArrivalTime,
			ClassNo:  item.ClassAvail,
		})
	}
	passengers := make([]omodel.PassengerFlightOrder, 0)
	for _, passenger := range data.Passenger {
		passengers = append(passengers, omodel.PassengerFlightOrder{
			Surname:                  passenger.FirstName,
			GivenNames:               passenger.LastName,
			Gender:                   travelskypnr.GenderMap[passenger.Gender],
			Birthday:                 passenger.Birthday,
			TravelDocument:           travelskypnr.TravelDocumentMap[passenger.IDCardType],
			TravelDocumentNumber:     passenger.IDCardNo,
			TravelDocumentExpireDate: passenger.IDExpireDate,
			Type:                     travelskypnr.PassengerTypeMap[passenger.Type],
			Nationality:              passenger.Nationality,
			//Region: ,
			//Phone: ,
			//Email: ,
			//CardNo: ,
			//Airline: ,
		})
	}
	flightDetail.Flights = journeys
	flightDetail.Passengers = passengers
	flightDetail.Airline = airline
	return
}
